﻿using Nova_Common.Extend;
using Nova_DAL;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nova_BLL
{
    public class RFBll
    {
        RFDal dal = new RFDal();
        TBase_InterfaceDal Interface_Dal = new TBase_InterfaceDal();

        /// <summary>
        /// 获取托盘组盘的物料信息
        /// </summary>
        /// <param name="trayId">托盘码</param>
        /// <returns></returns>
        public DataTable GetTrayMaterialInfor(string trayId)
        {
            return dal.GetTrayMaterialInfor(trayId);
        }

        /// <summary>
        /// 获取托盘组盘的特殊物品物料信息
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        public DataTable GetTraySpecialMaterialInfor(string trayId)
        {
            return dal.GetTraySpecialMaterialInfor(trayId);
        }

        /// <summary>
        /// 获取未处理入库单据列表
        /// </summary>
        /// <returns></returns>
        public DataTable DataListStorage()
        {
            return dal.DataListStorage();
        }

        /// <summary>
        /// 获取托盘在立体库中的信息
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        public DataTable TrayCanPackage(string trayId)
        {
            return dal.TrayCanPackage(trayId);
        }

        /// <summary>
        /// 物料编码是否存在物料表中
        /// </summary>
        /// <param name="matCode"></param>
        public bool MaterialCanPackage(string matCode)
        {
            return dal.MaterialCanPackage(matCode);
        }

        /// <summary>
        /// 获取单据物料信息
        /// </summary>
        /// <param name="StockNo"></param>
        /// <returns></returns>
        public DataTable DataListMaterial(string stockNo)
        {
            return dal.DataListMaterial(stockNo);
        }

        /// <summary>
        /// 获取入库单据上物料剩余数量
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="MatCode"></param>
        /// <returns></returns>
        public int GetMaterialReNum(string stockNo, string matCode)
        {
            return dal.GetMaterialReNum(stockNo, matCode);
        }

        /// <summary>
        /// 获取入库单据上物料的数量
        /// </summary>
        /// <param name="stockNo"></param>
        /// <param name="sec_Mat_Id"></param>
        /// <returns></returns>
        public DataTable GetAddStorageMaterialInfo(string stockNo, int sec_Mat_Id,string pid)
        {
            return dal.GetAddStorageMaterialInfo(stockNo, sec_Mat_Id,pid);
        }

        /// <summary>
        /// 组盘
        /// </summary>
        /// <param name="stockNo"></param>
        /// <param name="trayId"></param>
        /// <param name="matCode"></param>
        /// <param name="secMatId"></param>
        /// <param name="matNum"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <returns></returns>
        public StoreProceResult SetPackage(string stockNo, string trayId, string matCode, long secMatId, decimal matNum, int TypeOfSize, int TypeOfWeight = 0)
        {
            StoreProceResult res = dal.PackageCommon(stockNo, matCode, trayId, secMatId, matNum, TypeOfSize, TypeOfWeight);
            return res;
        }

        /// <summary>
        /// 补货
        /// </summary>
        /// <param name="stockNo"></param>
        /// <param name="trayId"></param>
        /// <param name="matCode"></param>
        /// <param name="secMatId"></param>
        /// <param name="matNum"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <returns></returns>
        public StoreProceResult ReplenPackage(long taskId,string stockNo, string trayId, string matCode, long secMatId, decimal matNum, int TypeOfSize, int TypeOfWeight = 0)
        {
            StoreProceResult res = dal.ReplenPackageCommon(taskId,stockNo, matCode, trayId, secMatId, matNum, TypeOfSize, TypeOfWeight);
            return res;
        }

        /// <summary>
        /// 特殊物料组盘-不根据入库单
        /// </summary>
        /// <param name="trayId"></param>
        /// <param name="matCode"></param>
        /// <param name="matNum"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <returns></returns>
        public StoreProceResult SetPackageSpecial( string trayId, string matCode, decimal matNum, int TypeOfSize, int TypeOfWeight = 0)
        {
             // 获取物料的二级编码
            DataTable dt = Interface_Dal.GetInfoByMatCode(matCode);
            long set_Mat_Code = 0;
            if (dt.Rows.Count > 0)
                set_Mat_Code = int.Parse(dt.Rows[0]["Sec_Mat_ID"].ToString());
            else
                set_Mat_Code = Interface_Dal.Insert(matCode);
            StoreProceResult res = dal.PackageCommonSpecial(matCode, trayId, set_Mat_Code, matNum, TypeOfSize, TypeOfWeight);
            return res;
        }

        /// <summary>
        /// 解盘
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="trayId"></param>
        /// <param name="SecMatId"></param>
        /// <param name="MatNum"></param>
        /// <returns></returns>
        public StoreProceResult PackageDel(string StockNo, string trayId, long SecMatId, decimal MatNum)
        {
            string containerID = dal.GetStorageKey(trayId, SecMatId);
            if (containerID.IsEmpty())
                throw new Exception("物料不存在托盘上，无需解绑!");
            StoreProceResult res = dal.PackageDel(StockNo, containerID, SecMatId, MatNum);
            return res;
        }

        /// <summary>
        /// 解盘-特殊物料
        /// </summary>
        /// <param name="ContainerID"></param>
        /// <param name="SecMatId"></param>
        /// <param name="MatNum"></param>
        /// <returns></returns>
        public StoreProceResult PackageDelSpecial(string trayId, string matCode, decimal MatNum)
        {
            DataTable dt = Interface_Dal.GetInfoByMatCode(matCode);
            long secMatCode = 0;
            if (dt.Rows.Count > 0)
            {
               secMatCode = long.Parse(dt.Rows[0]["Sec_Mat_ID"].ToString());
            }
            string containerID = dal.GetStorageKey(trayId, secMatCode);
            if (containerID.IsEmpty())
                throw new Exception("物料不存在托盘上，无需解绑!");
            StoreProceResult res = dal.PackageDelSpecial(containerID, secMatCode, MatNum);
            return res;
        }

        /// <summary>
        /// 根据托盘码获取盘点指令,物料等出库信息
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        public DataTable GetCheckCommandMatInfoByTrayId(string trayId)
        {
            return dal.GetCheckCommandMatInfoByTrayId(trayId);
        }

        /// <summary>
        /// 根据托盘码获取补货指令，物料等出库信息
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        public DataTable GetReplenCommandMatInfoByTrayId(string trayId)
        {
            return dal.GetReplenCommandMatInfoByTrayId(trayId);
        }

        /// <summary>
        /// 根据托盘码获取拣选指令，物料等出库信息
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        public DataTable GetChooseCommandMatInfoByTrayId(string trayId)
        {
            return dal.GetChooseCommandMatInfoByTrayId(trayId);
        }

        public decimal GetTrayMatNumBySectId(string trayId, long secMatId)
        {
            return dal.GetTrayMatNumBySectId(trayId, secMatId);
        }

        public int GetRelepenCmdRecordByTrayId(string trayId)
        {
            return dal.GetRelepenCmdRecordByTrayId(trayId);
        }

        public DataTable GetReplenCommandRecordByTrayId(string trayId)
        {
            return dal.GetReplenCommandRecordByTrayId(trayId);
        }

        public StoreProceResult ReplenStorageOverWeight(long taskId, string strStockNo, string strTrayId, long SecMatId, decimal MatNum)
        {
            return dal.ReplenStorageOverWeight(taskId, strStockNo, strTrayId, SecMatId, MatNum);
        }
    }
}
